﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Quartz;
using OpenPop.Mime;
using System.Data.SqlClient;
using System.IO;
using System.Net.Mail;
using System.Net;
using System.Data;
using Common.Logging;
namespace EmailSchedule
{
     public class sendEmailToQueue:IJob
    {
         private static ILog _log = LogManager.GetLogger(typeof(sendEmailToQueue));
         public virtual void Execute(IJobExecutionContext context)
         {
             //List<Message> listMessage = null;
             //int i = 0;
             //SqlConnection _conn = new SqlConnection(Helper.sqlConnectString);
             //_conn.Open();
             //string selectQuery = string.Format(@"Select top 1 Content from Email Where Status='R'");
             //SqlCommand selectCommand = new SqlCommand(selectQuery, _conn);
             //SqlDataReader reader = selectCommand.ExecuteReader();
             //MessageInfo message;
             //while (reader.Read())
             //{
             //    byte[] messageArray = (byte[])reader[0];
             //    Stream messageStream = new MemoryStream(messageArray);
             //    Message loadedMessage = Message.Load(messageStream);
             //    if (Helper.PendingMessageQueue.Count<10000)
             //    {       
             //        Helper.PendingMessageQueue.Enqueue(loadedMessage);
             //    }              
         
             //}
             _log.Info(string.Format("sendEmailToQueue! - {0}", System.DateTime.Now.ToString("r")));
             MessageInfo message = new MessageInfo();
             try
             {
                 using (SqlConnection cn = new SqlConnection(Helper.sqlConnectString))
                 {
                     cn.Open();
                     using (SqlCommand cm = cn.CreateCommand())
                     {
                         cm.CommandText = "INQUIRY_EMAIL";
                         cm.CommandType = CommandType.StoredProcedure;
                         using (SqlDataReader reader = cm.ExecuteReader())
                         {
                             while (reader.Read())
                             {
                                 if (check_Data(reader["MessageId"].ToString()))
                                 {
                                     message.MessageId = reader["MessageId"].ToString();
                                     message.Subject = reader["Subject"].ToString();
                                     message.FromEmail = reader["FromEmail"].ToString();
                                     message.ToEmail = reader["ToEmail"].ToString();
                                     message.FromName = reader["FromName"].ToString();
                                     if (Helper.PendingMessageQueue.Count < 10000)
                                     {
                                         Helper.MessageIdList.Add(message.MessageId);
                                         Helper.PendingMessageQueue.Enqueue(message);
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
             catch (Exception ex)
             {
                 Helper.LogToFile(LogFileType.EXCEPTION, " sendEmailToQueue -" + ex.Message,Helper.sendEmailToQueue);
             }
         }
         public bool check_Data(string messageId)
         {
             try
             {
                 if (Helper.MessageIdList.Count > 0)
                 {
                     foreach (string item in Helper.MessageIdList)
                     {
                         if (item == messageId)
                         {
                             return false;
                         }
                     }
                 }
                 return true;
             }
             catch(Exception ex)
             {
                 return true;
             }
         }
    }
}
